<?php
/*****************************************
 * This file is part of SDM Dynamo CMS   *
 * Copyright (c) Sean Micklethwaite 2006 *
 * All rights reserved                   *
 ****************************************/

define("ENONE", 0);
define("ENOTFOUND", -1);
define("EAUTH", -2);
define("EDENIED", -3);
define("EINVALIDARGS", -4);
define("EDB", -5);
define("ENOTIMPLEMENTED", -6);
define("EINVALIDOP", -7);
define("EDUP", -8);

require_once("conf.php");

global $gStartTime;
global $gLog;
global $gDycmsVersion;
global $gBrowserUrl, $gViewerUrl, $gEditorUrl;
global $gSite;

abstract class cDycmsAbstractObject
{
	protected 	$mAttributes;
	protected	$mId;
	protected	$mClsId;
	protected	$mbModified;

	function __construct()
	{
		$this->mbModified = true;
	}

	function Read()
	{
		$this->mbModified = false;
	}

	function Write()
	{
		$this->mbModified = false;
	}

	function __destruct()
	{
		if($this->mbModified)
			$this->Write();
	}
}

require_once("core/data-".DB.".php");
require_once("core/class.php");
require_once("core/object.php");
require_once("core/query.php");
require_once("core/security.php");
require_once("core/vfs.php");
require_once("core/client.php");
require_once("ui/ui.php");


function dyInit($site = '', $browser = BROWSER, $viewer = VIEWER, $editor = EDITOR)
{
	global $gStartTime, $gLog, $gDycmsVersion;
	global $gBrowserUrl, $gViewerUrl, $gEditorUrl;
	global $gDataPrefix;
	global $gSite;
	
	session_start();
	error_reporting(E_ALL ^ E_NOTICE);

	$gStartTime = microtime(true);
	$gLog = array();
	dyLog("Begin system initialization...");


	$gDycmsVersion = "DyCMS v0.3.3 (beta)";
	$gBrowserUrl = dyFormatUrl($browser);
	$gViewerUrl = dyFormatUrl($viewer);
	$gEditorUrl = dyFormatUrl($editor);
	$gSite = $site;
	
	dbInit();
	dyInitSecurity();
	uiInit();
	dyLog("System initialized");
}

function dyInitQuiet()
{
	global $gLog;

	$gLog = array();
	session_start();
	error_reporting(E_NONE);
	dbInit();
	dyInitSecurity();
	uiInit();
}

function dyEnd()
{
	global $gStartTime;

	dyEndSecurity();

	//
	// Close the system log
	//

	$t = microtime(true);
	$t -= $gStartTime;
	$t = round($t, 5);
	dyLog("Request completed in $t seconds");
	//dyLogDump();
}

function dyEndQuiet()
{
	dyEndSecurity();
}


/**** URL Processing ****/

function dyFormatUrl($url)
{
	$l = strlen($url) - 1;
	if($url[$l] == ';' && substr($url, $l-4,5) == '&amp;')
		$url = substr($url, 0,$l-4);
	else if($url[$l] == '&' || $url[$l] == '?')
		$url = substr($url, 0,$l);
	return $url;
}

function dyBrowserUrl($argv = NULL)
{
	global $gBrowserUrl;
	if(!$argv)
		return $gBrowserUrl;

	if(strpos($gBrowserUrl, '?') === false)
		$s = '?';
	else
		$s = '&';

	if(is_array($argv))
	{
		foreach($argv as $key => $val)
		{
			$sz[$key] .= urlencode($key)."=".urlencode($val);
		};

		$s .= implode('&amp;', $sz);
	}
	else
	{
		$s .= '$p='.urlencode($argv);
	};

	return ($gBrowserUrl.$s);
}

function dyViewerUrl($clsid = NULL, $id = NULL, $argv = array())
{
	global $gViewerUrl;
	if(!$clsid)
		return $gViewerUrl;

	if(strpos($gViewerUrl, '?') === false)
		$s = "?";
	else
		$s = "&";

	$s .= "\$clsid=$clsid";
	if($id)
		$s .= "&\$id=$id";
	if($argv === true){
		$argv = array();
		foreach($_GET as $k => $v){
			if($k[0] == '$' && $k[1] == '$')
				$argv[$k] = $v;
		};
	};
	if(is_array($argv))
	{
		foreach($argv as $key => $val)
		{
			$s .= '&'.urlencode($key).'='.urlencode($val);
		};
	};
	
	return ($gViewerUrl.$s);
}

function dyEditorUrl($clsid = NULL, $id = NULL, $argv = NULL)
{
	global $gEditorUrl;
	if(!$clsid)
		return $gEditorUrl;

	if(strpos($gEditorUrl, '?') === false)
		$s = "?";
	else
		$s = '&';

	$s .= "\$clsid=$clsid";
	if($id)
		$s .= "&\$id=$id";
	if($argv === true){
		$argv = array();
		foreach($_GET as $k => $v){
			if($k[0] == '$' && $k[1] == '$')
				$argv[$k] = $v;
		};
	};
	if(is_array($argv))
	{
		foreach($argv as $key => $val)
		{
			$s .= '&'.urlencode($key).'='.urlencode($val);
		};
	};

	return ($gEditorUrl.$s);
}

function dyLog($msg)
{
	array_push($GLOBALS["gLog"], microtime(true).":::".$msg);
};

function dyLogDump()
{
	echo "<h2>Dumping System Log</h2>\n<table border='1'>\n".
		"<tr><th>Time</th><th>Log Entry</th></tr>";
	foreach($GLOBALS["gLog"] as $log)
	{
		$logz = split(":::", $log);
		echo "<tr><td>".$logz[0]."</td><td>".$logz[1]."</td></tr>\n";
	};
	echo("</table>");
};

function dyDie($msg)
{
	uiErrorCode(0);
	echo "<p>Message: $msg</p>";
	dyLogDump();
	die();
};

function dyPostOrGet($i){
    return (isset($_POST[$i]) ? $_POST[$i] : $_GET[$i]);
}

class dyException extends Exception
{
	function __construct($s_msg = null, $i_code = 0)
	{
		parent::__construct($s_msg, $i_code);
	}

	function __toString()
	{
		return nl2br(parent::__toString());
	}
}

class dyExNotFound extends dyException
{
	function __construct($s_msg = 'unknown')
	{
		parent::__construct($s_msg, ENOTFOUND);
	}
}

class dyExInvalidOp extends dyException
{
	function __construct($s_msg = null)
	{
		parent::__construct($s_msg, EINVALIDOP);
	}
}

class dyExDbError extends dyException
{
	function __construct($s_msg = null)
	{
		parent::__construct($s_msg, EDB);
	}
}

class dyExDenied extends dyException
{
	function __construct($s_msg = null)
	{
		parent::__construct($s_msg, EDENIED);
	}
}

class dyExAuth extends dyException
{
	function __construct($s_msg = null)
	{
		parent::__construct($s_msg, EAUTH);
	}
}

class dyExValidation extends dyException
{
	function __construct($s_msg = null)
	{
		parent::__construct($s_msg, EINVALIDARGS);
	}
}

class dyExDuplicate extends dyException
{
	function __construct($s_msg = null)
	{
		parent::__construct($s_msg, EDUP);
	}
}

?>
